from typing import Optional, List

from fastapi import APIRouter, Request
from fastapi import Depends
from sqlalchemy.orm import Session

from config.get_db import get_db
from core.dao.cms_car_dao import CmsCarDao
from core.dao.cms_order_execute_dao import CmsOrderExecuteDao
from core.entity.vo.common import CrudResponse
from core.entity.vo.view_models import CmsTerminalPointVO, TerminalPointQuery, OrderQuery, CmsTerminalPointVO2
from core.service.cms_terminal_point_service import CmsTerminalPointService
from models import CmsOrderExecute, CmsCar, CmsTerminalPoint
from utils.log_util import *
from utils.response_util import *

terminalPointController = APIRouter()


@terminalPointController.post("/terminal_point/query_by_id", response_model=CmsTerminalPointVO)
async def get_terminal_point_by_id(request: Request, id: int, query_db: Session = Depends(get_db)):
    try:
        # 获取全量数据
        cms_terminal_point = CmsTerminalPointService.get_cms_terminal_point_by_id(query_db, id)
        logger.info('获取成功')
        return response_200(data=cms_terminal_point, message="获取成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@terminalPointController.post("/terminal_point/query_by_order_id", response_model=CmsTerminalPointVO2)
async def get_terminal_point_by_order_id(
        query: OrderQuery,
        query_db: Session = Depends(get_db)
):
    try:
        # 使用query.order_id, query.start_time, query.end_time进行查询
        cms_terminal_point: List[CmsTerminalPoint] = CmsTerminalPointService.get_cms_terminal_point_by_order_id(
            query_db, query.order_id
        )
        car_number = ""
        order_id: CmsOrderExecute = CmsOrderExecuteDao.get_one_by_order_id(query_db, query.order_id)
        if order_id:
            car_id: CmsCar = CmsCarDao.get_car_by_car_id(query_db, order_id.car_id)
            if car_id:
                car_number = car_id.car_number
        cms_terminal_point_list = []
        for e in cms_terminal_point:
            vo_ = CmsTerminalPointVO2(**e)
            vo_.car_number = car_number
            cms_terminal_point_list.append(vo_)
        logger.info('获取成功')
        return response_200(data=cms_terminal_point_list, message="获取成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@terminalPointController.post("/terminal_point/query_by_terminal_id", response_model=CmsTerminalPointVO)
async def get_terminal_point_by_terminal_id(
        request: Request,
        query: TerminalPointQuery,
        query_db: Session = Depends(get_db)
):
    try:
        # 使用query.terminal_id, query.start_time, query.end_time进行查询
        cms_terminal_point = CmsTerminalPointService.get_cms_terminal_point_by_terminal_id(
            query_db, query.terminal_id, query.start_time, query.end_time
        )
        return response_200(data=cms_terminal_point, message="获取成功")
    except Exception as e:
        return response_500(data="", message=str(e))


@terminalPointController.post("/terminal_point/add", response_model=CrudResponse)
async def add_terminal_point(request: Request, cms_terminal_point: CmsTerminalPointVO,
                             query_db: Session = Depends(get_db)):
    try:
        cms_terminal_point_result = await CmsTerminalPointService.add_cms_terminal_point(request, query_db,
                                                                                         cms_terminal_point)
        if cms_terminal_point_result.is_success:
            logger.info(cms_terminal_point_result.message)
            return response_200(data=cms_terminal_point_result, message=cms_terminal_point_result.message)
        else:
            logger.warning(cms_terminal_point_result.message)
            return response_400(data="", message=cms_terminal_point_result.message)
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@terminalPointController.patch("/terminal_point/edit", response_model=CrudResponse)
async def edit_terminal_point(request: Request, cms_terminal_point: CmsTerminalPointVO,
                              query_db: Session = Depends(get_db)):
    try:
        cms_terminal_point_result = await CmsTerminalPointService.edit_cms_terminal_point(query_db, cms_terminal_point)
        if cms_terminal_point_result.is_success:
            logger.info(cms_terminal_point_result.message)
            return response_200(data=cms_terminal_point_result, message=cms_terminal_point_result.message)
        else:
            logger.warning(cms_terminal_point_result.message)
            return response_400(data="", message=cms_terminal_point_result.message)
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@terminalPointController.post("/terminal_point/delete", response_model=CrudResponse)
async def delete_terminal_point(request: Request, id: int, query_db: Session = Depends(get_db)):
    try:
        delete_terminal_point_result = CmsTerminalPointService.delete_cms_terminal_point(query_db, id)
        if delete_terminal_point_result.is_success:
            logger.info(delete_terminal_point_result.message)
            return response_200(data=delete_terminal_point_result, message=delete_terminal_point_result.message)
        else:
            logger.warning(delete_terminal_point_result.message)
            return response_400(data="", message=delete_terminal_point_result.message)
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))
